package com.mission.schedule.activity;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.mission.schedule.R;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.constants.Const;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.cutimage.Crop;
import com.mission.schedule.utils.ActivityManager1;
import com.mission.schedule.utils.AfterPermissionGranted;
import com.mission.schedule.utils.EasyPermissions;
import com.mission.schedule.utils.ImageUtils;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.PathFromUriUtils;
import com.mission.schedule.utils.PhotoActionHelper;
import com.mission.schedule.utils.RxPhotoUtils;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.utils.Utils;
import com.yalantis.ucrop.UCrop;

import org.w3c.dom.Text;

import java.io.File;
import java.util.List;

public class SetBackgroundActivity extends BaseActivity implements
        OnClickListener, EasyPermissions.PermissionCallbacks {

    private LinearLayout top_ll_left, dbj_ll;
    private RelativeLayout nonebg_rl, item1_bg_rl, item2_bg_rl, item3_bg_rl, item4_bg_rl, item5_bg_rl, item6_bg_rl, item7_bg_rl, item8_bg_rl, selectlocal_rl;
    private CheckBox select0_cb, select1_cb, select2_cb, select3_cb, select4_cb, select5_cb, select6_cb, select7_cb, select8_cb;

    private ToggleButton toggle_gh, toggle_sj;
    private RelativeLayout sj_rl;
    private TextView tishi_tv, week1, week2, week3, week4, week5, week6, week7;
    private ImageView local_img, image0, image1, image2, image3, image4, image5, image6, image7, image8;

    Context context;
    SharedPrefUtil sharedPrefUtil = null;
    String localpath = "", ismostimage = "", issjmostimage = "";
    Bitmap bitmap;
    String paths;
    private static final int REQUEST_CODE_CAPTURE_CAMEIA = 1458;
    private String mOutputPath;
    private String mDemoPath;
    ActivityManager1 activityManager = null;
    boolean permission = false;

    @Override
    protected void setListener() {
        top_ll_left.setOnClickListener(this);
        nonebg_rl.setOnClickListener(this);
        item1_bg_rl.setOnClickListener(this);
        item2_bg_rl.setOnClickListener(this);
        item3_bg_rl.setOnClickListener(this);
        item4_bg_rl.setOnClickListener(this);
        item5_bg_rl.setOnClickListener(this);
        item6_bg_rl.setOnClickListener(this);
        item7_bg_rl.setOnClickListener(this);
        item8_bg_rl.setOnClickListener(this);
        selectlocal_rl.setOnClickListener(this);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_setbackground);
    }

    @Override
    protected void onResume() {
        super.onResume();
        localpath = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "1");
        if (localpath.length() > 1) {
            local_img.setImageBitmap(BitmapFactory.decodeFile(localpath));
        }
        ismostimage = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "");
        issjmostimage = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISSJMOSTIMAGE, "");
        setItembg();

    }

    @Override
    protected void init(Bundle savedInstanceState) {
        activityManager = ActivityManager1.getInstance();
        activityManager.addActivities(this);
        context = this;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "");
        localpath = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "1");
        ismostimage = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "");
        issjmostimage = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISSJMOSTIMAGE, "");

        select0_cb = (CheckBox) findViewById(R.id.select0_cb);
        select1_cb = (CheckBox) findViewById(R.id.select1_cb);
        select2_cb = (CheckBox) findViewById(R.id.select2_cb);
        select3_cb = (CheckBox) findViewById(R.id.select3_cb);
        select4_cb = (CheckBox) findViewById(R.id.select4_cb);
        select5_cb = (CheckBox) findViewById(R.id.select5_cb);
        select6_cb = (CheckBox) findViewById(R.id.select6_cb);
        select7_cb = (CheckBox) findViewById(R.id.select7_cb);
        select8_cb = (CheckBox) findViewById(R.id.select8_cb);
        item1_bg_rl = (RelativeLayout) findViewById(R.id.item1_bg_rl);
        item2_bg_rl = (RelativeLayout) findViewById(R.id.item2_bg_rl);
        item3_bg_rl = (RelativeLayout) findViewById(R.id.item3_bg_rl);
        item4_bg_rl = (RelativeLayout) findViewById(R.id.item4_bg_rl);
        item5_bg_rl = (RelativeLayout) findViewById(R.id.item5_bg_rl);
        item6_bg_rl = (RelativeLayout) findViewById(R.id.item6_bg_rl);
        item7_bg_rl = (RelativeLayout) findViewById(R.id.item7_bg_rl);
        item8_bg_rl = (RelativeLayout) findViewById(R.id.item8_bg_rl);

        image0 = (ImageView) findViewById(R.id.image0);
        image1 = (ImageView) findViewById(R.id.image1);
        image2 = (ImageView) findViewById(R.id.image2);
        image3 = (ImageView) findViewById(R.id.image3);
        image4 = (ImageView) findViewById(R.id.image4);
        image5 = (ImageView) findViewById(R.id.image5);
        image6 = (ImageView) findViewById(R.id.image6);
        image7 = (ImageView) findViewById(R.id.image7);
        image8 = (ImageView) findViewById(R.id.image8);

        week1 = (TextView) findViewById(R.id.week1);
        week2 = (TextView) findViewById(R.id.week2);
        week3 = (TextView) findViewById(R.id.week3);
        week4 = (TextView) findViewById(R.id.week4);
        week5 = (TextView) findViewById(R.id.week5);
        week6 = (TextView) findViewById(R.id.week6);
        week7 = (TextView) findViewById(R.id.week7);

        nonebg_rl = (RelativeLayout) findViewById(R.id.nonebg_rl);
        selectlocal_rl = (RelativeLayout) findViewById(R.id.selectlocal_rl);
        sj_rl = (RelativeLayout) findViewById(R.id.sj_rl);

        top_ll_left = (LinearLayout) findViewById(R.id.top_ll_left);
        dbj_ll = (LinearLayout) findViewById(R.id.dbj_ll);
        toggle_gh = (ToggleButton) findViewById(R.id.toggle_gh);
        toggle_sj = (ToggleButton) findViewById(R.id.toggle_sj);
        tishi_tv = (TextView) findViewById(R.id.tishi_tv);
        local_img = (ImageView) findViewById(R.id.local_img);


        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getWindowManager().getDefaultDisplay().getWidth());
        params.setMargins(0, Utils.dipTopx(this, 15), 0, 0);
//        params.gravity = Gravity.CENTER;
        dbj_ll.setLayoutParams(params);
        dbj_ll.setPadding(Utils.dipTopx(this, 15), 0 , Utils.dipTopx(this, 15), 0);
//
//        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams((getWindowManager().getDefaultDisplay().getWidth() - (Utils.dipTopx(this, 60)))/3,
//                ViewGroup.LayoutParams.MATCH_PARENT);
//        params1.weight = 1;
//        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams((getWindowManager().getDefaultDisplay().getWidth() - (Utils.dipTopx(this, 60)))/3,
//                ViewGroup.LayoutParams.MATCH_PARENT);
//        params2.weight = 1;
//        params2.setMargins(Utils.dipTopx(this, 15), 0, 0, 0);
//        nonebg_rl.setLayoutParams(params1);
//        item1_bg_rl.setLayoutParams(params2);
//        item2_bg_rl.setLayoutParams(params2);
//        item3_bg_rl.setLayoutParams(params1);
//        item4_bg_rl.setLayoutParams(params2);
//        item5_bg_rl.setLayoutParams(params2);
//        item6_bg_rl.setLayoutParams(params1);
//        item7_bg_rl.setLayoutParams(params2);
//        item8_bg_rl.setLayoutParams(params2);

        initview();

        if (localpath.length() > 1) {
            local_img.setImageBitmap(BitmapFactory.decodeFile(localpath));
        }

        if (ismostimage.isEmpty()) {
            sj_rl.setVisibility(View.GONE);
            selectlocal_rl.setVisibility(View.VISIBLE);
            toggle_gh.setChecked(false);
            toggle_sj.setChecked(false);
        } else {
            tishi_tv.setText("根据星期每天更换日程背景！点击图片可更换图片");
            selectlocal_rl.setVisibility(View.GONE);
            toggle_gh.setChecked(true);
            image8.setVisibility(View.GONE);
            image7.setVisibility(View.GONE);
            sj_rl.setVisibility(View.VISIBLE);
            localpath = "";
            setItembg();
            initview();
            if (!issjmostimage.isEmpty()) {
                toggle_sj.setChecked(true);
                tishi_tv.setText("开启后！每次从以下图片中随机选择一张作为背景");
            }
        }

        toggle_gh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    selectlocal_rl.setVisibility(View.GONE);
                    sj_rl.setVisibility(View.VISIBLE);
                    tishi_tv.setText("根据星期每天更换日程背景！点击图片可更换图片");
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "1");
                    image8.setVisibility(View.GONE);
                    image7.setVisibility(View.GONE);
                    localpath = "";
                    setItembg();
                    initview();
                } else {
                    image8.setVisibility(View.VISIBLE);
                    image7.setVisibility(View.VISIBLE);
                    tishi_tv.setText("推荐背景");
                    sj_rl.setVisibility(View.GONE);
                    selectlocal_rl.setVisibility(View.VISIBLE);
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "");
                    localpath = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "1");
                    setItembg();
                    initview();
                }
            }
        });
        toggle_sj.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    week1.setVisibility(View.GONE);
                    week2.setVisibility(View.GONE);
                    week3.setVisibility(View.GONE);
                    week4.setVisibility(View.GONE);
                    week5.setVisibility(View.GONE);
                    week6.setVisibility(View.GONE);
                    week7.setVisibility(View.GONE);
                    tishi_tv.setText("开启后！每次从以下图片中随机选择一张作为背景");
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISSJMOSTIMAGE, "1");
                } else {
                    week1.setVisibility(View.VISIBLE);
                    week2.setVisibility(View.VISIBLE);
                    week3.setVisibility(View.VISIBLE);
                    week4.setVisibility(View.VISIBLE);
                    week5.setVisibility(View.VISIBLE);
                    week6.setVisibility(View.VISIBLE);
                    week7.setVisibility(View.VISIBLE);
                    tishi_tv.setText("根据星期每天更换日程背景！点击图片可更换图片");
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISSJMOSTIMAGE, "");
                }
            }
        });
    }

    private void setItembg() {
        if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
            item8_bg_rl.setVisibility(View.VISIBLE);
            nonebg_rl.setBackground(getResources().getDrawable(R.mipmap.aa1));
            image1.setBackground(getResources().getDrawable(R.mipmap.a1));
            image2.setBackground(getResources().getDrawable(R.mipmap.b1));
            image3.setBackground(getResources().getDrawable(R.mipmap.c1));
            image4.setBackground(getResources().getDrawable(R.mipmap.d1));
            image5.setBackground(getResources().getDrawable(R.mipmap.e1));
            image6.setBackground(getResources().getDrawable(R.mipmap.f1));
            image7.setBackground(getResources().getDrawable(R.mipmap.g1));
            image8.setBackground(getResources().getDrawable(R.mipmap.h1));

            image1.setImageBitmap(null);
            image2.setImageBitmap(null);
            image3.setImageBitmap(null);
            image4.setImageBitmap(null);
            image5.setImageBitmap(null);
            image6.setImageBitmap(null);

            week1.setVisibility(View.GONE);
            week2.setVisibility(View.GONE);
            week3.setVisibility(View.GONE);
            week4.setVisibility(View.GONE);
            week5.setVisibility(View.GONE);
            week6.setVisibility(View.GONE);
            week7.setVisibility(View.GONE);
            image0.setVisibility(View.GONE);
        } else {
            image0.setVisibility(View.VISIBLE);
            if (issjmostimage.isEmpty()) {
                week1.setVisibility(View.VISIBLE);
                week2.setVisibility(View.VISIBLE);
                week3.setVisibility(View.VISIBLE);
                week4.setVisibility(View.VISIBLE);
                week5.setVisibility(View.VISIBLE);
                week6.setVisibility(View.VISIBLE);
                week7.setVisibility(View.VISIBLE);
            } else {
                week1.setVisibility(View.GONE);
                week2.setVisibility(View.GONE);
                week3.setVisibility(View.GONE);
                week4.setVisibility(View.GONE);
                week5.setVisibility(View.GONE);
                week6.setVisibility(View.GONE);
                week7.setVisibility(View.GONE);
            }
            if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE1, "").isEmpty()) {
                image0.setBackground(getResources().getDrawable(R.mipmap.a1));
            } else {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE1, "0").length() == 1) {
                    image0.setBackground(getD(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE1, "0")));
                } else {
                    image0.setImageBitmap(BitmapFactory.decodeFile(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE1, "")));
                }
            }
            if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE2, "").isEmpty()) {
                image1.setBackground(getResources().getDrawable(R.mipmap.b1));
            } else {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE2, "a").length() == 1) {
                    image1.setBackground(getD(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE2, "a")));
                } else {
                    image1.setImageBitmap(BitmapFactory.decodeFile(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE2, "")));
                }
            }
            if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE3, "").isEmpty()) {
                image2.setBackground(getResources().getDrawable(R.mipmap.c1));
            } else {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE3, "b").length() == 1) {
                    image2.setBackground(getD(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE3, "b")));
                } else {
                    image2.setImageBitmap(BitmapFactory.decodeFile(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE3, "")));
                }
            }
            if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE4, "").isEmpty()) {
                image3.setBackground(getResources().getDrawable(R.mipmap.d1));
            } else {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE4, "c").length() == 1) {
                    image3.setBackground(getD(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE4, "c")));
                } else {
                    image3.setImageBitmap(BitmapFactory.decodeFile(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE4, "")));
                }
            }
            if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE5, "").isEmpty()) {
                image4.setBackground(getResources().getDrawable(R.mipmap.e1));
            } else {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE5, "d").length() == 1) {
                    image4.setBackground(getD(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE5, "d")));
                } else {
                    image4.setImageBitmap(BitmapFactory.decodeFile(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE5, "")));
                }
            }
            if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE6, "").isEmpty()) {
                image5.setBackground(getResources().getDrawable(R.mipmap.f1));
            } else {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE6, "d").length() == 1) {
                    image5.setBackground(getD(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE6, "e")));
                } else {
                    image5.setImageBitmap(BitmapFactory.decodeFile(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE6, "")));
                }
            }
            if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE7, "").isEmpty()) {
                image6.setBackground(getResources().getDrawable(R.mipmap.g1));
            } else {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE7, "f").length() == 1) {
                    image6.setBackground(getD(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE7, "f")));
                } else {
                    image6.setImageBitmap(BitmapFactory.decodeFile(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE7, "")));
                }
            }
        }
    }

    private Drawable getD(String keycode) {
        Drawable drawable = null;
        switch (keycode) {
            case "0":
                drawable = getResources().getDrawable(R.mipmap.aa1);
                break;
            case "a":
                drawable = getResources().getDrawable(R.mipmap.a1);
                break;
            case "b":
                drawable = getResources().getDrawable(R.mipmap.b1);
                break;
            case "c":
                drawable = getResources().getDrawable(R.mipmap.c1);
                break;
            case "d":
                drawable = getResources().getDrawable(R.mipmap.d1);
                break;
            case "e":
                drawable = getResources().getDrawable(R.mipmap.e1);
                break;
            case "f":
                drawable = getResources().getDrawable(R.mipmap.f1);
                break;
            case "g":
                drawable = getResources().getDrawable(R.mipmap.g1);
                break;
            case "h":
                drawable = getResources().getDrawable(R.mipmap.h1);
                break;


        }

        return drawable;

    }

    private void initview() {
        if (!"".equals(localpath)) {
            if ("0".equals(localpath)) {
                select0_cb.setVisibility(View.VISIBLE);
                select1_cb.setVisibility(View.GONE);
                select2_cb.setVisibility(View.GONE);
                select3_cb.setVisibility(View.GONE);
                select4_cb.setVisibility(View.GONE);
                select5_cb.setVisibility(View.GONE);
                select6_cb.setVisibility(View.GONE);
                select7_cb.setVisibility(View.GONE);
                select8_cb.setVisibility(View.GONE);
                local_img.setBackground(context.getResources().getDrawable(R.mipmap.aa1));
                select0_cb.setChecked(true);
            } else if ("1".equals(localpath)) {
                select0_cb.setVisibility(View.GONE);
                select1_cb.setVisibility(View.VISIBLE);
                select2_cb.setVisibility(View.GONE);
                select3_cb.setVisibility(View.GONE);
                select4_cb.setVisibility(View.GONE);
                select5_cb.setVisibility(View.GONE);
                select6_cb.setVisibility(View.GONE);
                select7_cb.setVisibility(View.GONE);
                select8_cb.setVisibility(View.GONE);
                local_img.setBackground(context.getResources().getDrawable(R.mipmap.a1));
                select1_cb.setChecked(true);
            } else if ("2".equals(localpath)) {
                select0_cb.setVisibility(View.GONE);
                select1_cb.setVisibility(View.GONE);
                select2_cb.setVisibility(View.VISIBLE);
                select3_cb.setVisibility(View.GONE);
                select4_cb.setVisibility(View.GONE);
                select5_cb.setVisibility(View.GONE);
                select6_cb.setVisibility(View.GONE);
                select7_cb.setVisibility(View.GONE);
                select8_cb.setVisibility(View.GONE);
                local_img.setBackground(context.getResources().getDrawable(R.mipmap.b1));
                select2_cb.setChecked(true);
            } else if ("3".equals(localpath)) {
                select0_cb.setVisibility(View.GONE);
                select1_cb.setVisibility(View.GONE);
                select2_cb.setVisibility(View.GONE);
                select3_cb.setVisibility(View.VISIBLE);
                select4_cb.setVisibility(View.GONE);
                select5_cb.setVisibility(View.GONE);
                select6_cb.setVisibility(View.GONE);
                select7_cb.setVisibility(View.GONE);
                select8_cb.setVisibility(View.GONE);
                select3_cb.setChecked(true);
                local_img.setBackground(context.getResources().getDrawable(R.mipmap.c1));
            } else if ("4".equals(localpath)) {
                select0_cb.setVisibility(View.GONE);
                select1_cb.setVisibility(View.GONE);
                select2_cb.setVisibility(View.GONE);
                select3_cb.setVisibility(View.GONE);
                select4_cb.setVisibility(View.VISIBLE);
                select5_cb.setVisibility(View.GONE);
                select6_cb.setVisibility(View.GONE);
                select7_cb.setVisibility(View.GONE);
                select8_cb.setVisibility(View.GONE);
                local_img.setBackground(context.getResources().getDrawable(R.mipmap.d1));
                select4_cb.setChecked(true);
            } else if ("5".equals(localpath)) {
                select0_cb.setVisibility(View.GONE);
                select1_cb.setVisibility(View.GONE);
                select2_cb.setVisibility(View.GONE);
                select3_cb.setVisibility(View.GONE);
                select4_cb.setVisibility(View.GONE);
                select5_cb.setVisibility(View.VISIBLE);
                select6_cb.setVisibility(View.GONE);
                select7_cb.setVisibility(View.GONE);
                select8_cb.setVisibility(View.GONE);
                local_img.setBackground(context.getResources().getDrawable(R.mipmap.e1));
                select5_cb.setChecked(true);
            } else if ("6".equals(localpath)) {
                select0_cb.setVisibility(View.GONE);
                select1_cb.setVisibility(View.GONE);
                select2_cb.setVisibility(View.GONE);
                select3_cb.setVisibility(View.GONE);
                select4_cb.setVisibility(View.GONE);
                select5_cb.setVisibility(View.GONE);
                select6_cb.setVisibility(View.VISIBLE);
                select7_cb.setVisibility(View.GONE);
                select8_cb.setVisibility(View.GONE);
                local_img.setBackground(context.getResources().getDrawable(R.mipmap.f1));
                select6_cb.setChecked(true);
            } else if ("7".equals(localpath)) {
                select0_cb.setVisibility(View.GONE);
                select1_cb.setVisibility(View.GONE);
                select2_cb.setVisibility(View.GONE);
                select3_cb.setVisibility(View.GONE);
                select4_cb.setVisibility(View.GONE);
                select5_cb.setVisibility(View.GONE);
                select6_cb.setVisibility(View.GONE);
                select7_cb.setVisibility(View.VISIBLE);
                select8_cb.setVisibility(View.GONE);
                local_img.setBackground(context.getResources().getDrawable(R.mipmap.g1));
                select7_cb.setChecked(true);
            } else if ("8".equals(localpath)) {
                select0_cb.setVisibility(View.GONE);
                select1_cb.setVisibility(View.GONE);
                select2_cb.setVisibility(View.GONE);
                select3_cb.setVisibility(View.GONE);
                select4_cb.setVisibility(View.GONE);
                select5_cb.setVisibility(View.GONE);
                select6_cb.setVisibility(View.GONE);
                select7_cb.setVisibility(View.GONE);
                select8_cb.setVisibility(View.VISIBLE);
                local_img.setBackground(context.getResources().getDrawable(R.mipmap.h1));
                select8_cb.setChecked(true);
            } else {
                select0_cb.setVisibility(View.GONE);
                select1_cb.setVisibility(View.GONE);
                select2_cb.setVisibility(View.GONE);
                select3_cb.setVisibility(View.GONE);
                select4_cb.setVisibility(View.GONE);
                select5_cb.setVisibility(View.GONE);
                select6_cb.setVisibility(View.GONE);
                select7_cb.setVisibility(View.GONE);
                select8_cb.setVisibility(View.GONE);
            }
        } else {
            select0_cb.setVisibility(View.GONE);
            select1_cb.setVisibility(View.GONE);
            select2_cb.setVisibility(View.GONE);
            select3_cb.setVisibility(View.GONE);
            select4_cb.setVisibility(View.GONE);
            select5_cb.setVisibility(View.GONE);
            select6_cb.setVisibility(View.GONE);
            select7_cb.setVisibility(View.GONE);
            select8_cb.setVisibility(View.GONE);
        }
    }

    @Override
    protected void setAdapter() {

    }

    int week_image = 1;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_ll_left:
                this.finish();
                break;
            case R.id.nonebg_rl:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
                    select0_cb.setVisibility(View.VISIBLE);
                    select1_cb.setVisibility(View.GONE);
                    select2_cb.setVisibility(View.GONE);
                    select3_cb.setVisibility(View.GONE);
                    select4_cb.setVisibility(View.GONE);
                    select5_cb.setVisibility(View.GONE);
                    select6_cb.setVisibility(View.GONE);
                    select7_cb.setVisibility(View.GONE);
                    select8_cb.setVisibility(View.GONE);
                    select0_cb.setChecked(true);
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "0");
                    local_img.setBackground(context.getResources().getDrawable(R.mipmap.aa1));
                } else {
                    week_image = 1;
                    xuanzhetup("星期一");
                }
                break;
            case R.id.item1_bg_rl:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
                    select0_cb.setVisibility(View.GONE);
                    select1_cb.setVisibility(View.VISIBLE);
                    select2_cb.setVisibility(View.GONE);
                    select3_cb.setVisibility(View.GONE);
                    select4_cb.setVisibility(View.GONE);
                    select5_cb.setVisibility(View.GONE);
                    select6_cb.setVisibility(View.GONE);
                    select7_cb.setVisibility(View.GONE);
                    select8_cb.setVisibility(View.GONE);
                    select1_cb.setChecked(true);
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "1");
                    local_img.setBackground(context.getResources().getDrawable(R.mipmap.a1));
                } else {
                    week_image = 2;
                    xuanzhetup("星期二");
                }
                break;
            case R.id.item2_bg_rl:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
                    select0_cb.setVisibility(View.GONE);
                    select1_cb.setVisibility(View.GONE);
                    select2_cb.setVisibility(View.VISIBLE);
                    select3_cb.setVisibility(View.GONE);
                    select4_cb.setVisibility(View.GONE);
                    select5_cb.setVisibility(View.GONE);
                    select6_cb.setVisibility(View.GONE);
                    select7_cb.setVisibility(View.GONE);
                    select8_cb.setVisibility(View.GONE);
                    select2_cb.setChecked(true);
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "2");
                    local_img.setBackground(context.getResources().getDrawable(R.mipmap.b1));
                } else {
                    week_image = 3;
                    xuanzhetup("星期三");
                }
                break;
            case R.id.item3_bg_rl:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
                    select0_cb.setVisibility(View.GONE);
                    select1_cb.setVisibility(View.GONE);
                    select2_cb.setVisibility(View.GONE);
                    select3_cb.setVisibility(View.VISIBLE);
                    select4_cb.setVisibility(View.GONE);
                    select5_cb.setVisibility(View.GONE);
                    select6_cb.setVisibility(View.GONE);
                    select7_cb.setVisibility(View.GONE);
                    select8_cb.setVisibility(View.GONE);
                    select3_cb.setChecked(true);
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "3");
                    local_img.setBackground(context.getResources().getDrawable(R.mipmap.c1));
                } else {
                    week_image = 4;
                    xuanzhetup("星期四");
                }
                break;
            case R.id.item4_bg_rl:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
                    select0_cb.setVisibility(View.GONE);
                    select1_cb.setVisibility(View.GONE);
                    select2_cb.setVisibility(View.GONE);
                    select3_cb.setVisibility(View.GONE);
                    select4_cb.setVisibility(View.VISIBLE);
                    select5_cb.setVisibility(View.GONE);
                    select6_cb.setVisibility(View.GONE);
                    select7_cb.setVisibility(View.GONE);
                    select8_cb.setVisibility(View.GONE);
                    select4_cb.setChecked(true);
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "4");
                    local_img.setBackground(context.getResources().getDrawable(R.mipmap.d1));
                } else {
                    week_image = 5;
                    xuanzhetup("星期五");
                }
                break;
            case R.id.item5_bg_rl:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
                    select0_cb.setVisibility(View.GONE);
                    select1_cb.setVisibility(View.GONE);
                    select2_cb.setVisibility(View.GONE);
                    select3_cb.setVisibility(View.GONE);
                    select4_cb.setVisibility(View.GONE);
                    select5_cb.setVisibility(View.VISIBLE);
                    select6_cb.setVisibility(View.GONE);
                    select7_cb.setVisibility(View.GONE);
                    select8_cb.setVisibility(View.GONE);
                    select5_cb.setChecked(true);
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "5");
                    local_img.setBackground(context.getResources().getDrawable(R.mipmap.e1));
                } else {
                    week_image = 6;
                    xuanzhetup("星期六");
                }
                break;
            case R.id.item6_bg_rl:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
                    select0_cb.setVisibility(View.GONE);
                    select1_cb.setVisibility(View.GONE);
                    select2_cb.setVisibility(View.GONE);
                    select3_cb.setVisibility(View.GONE);
                    select4_cb.setVisibility(View.GONE);
                    select5_cb.setVisibility(View.GONE);
                    select6_cb.setVisibility(View.VISIBLE);
                    select7_cb.setVisibility(View.GONE);
                    select8_cb.setVisibility(View.GONE);
                    select6_cb.setChecked(true);
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "6");
                    local_img.setBackground(context.getResources().getDrawable(R.mipmap.f1));
                } else {
                    week_image = 7;
                    xuanzhetup("星期日");
                }
                break;
            case R.id.item7_bg_rl:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
                    select0_cb.setVisibility(View.GONE);
                    select1_cb.setVisibility(View.GONE);
                    select2_cb.setVisibility(View.GONE);
                    select3_cb.setVisibility(View.GONE);
                    select4_cb.setVisibility(View.GONE);
                    select5_cb.setVisibility(View.GONE);
                    select6_cb.setVisibility(View.GONE);
                    select7_cb.setVisibility(View.VISIBLE);
                    select8_cb.setVisibility(View.GONE);
                    select7_cb.setChecked(true);
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "7");
                    local_img.setBackground(context.getResources().getDrawable(R.mipmap.g1));
                }
                break;
            case R.id.item8_bg_rl:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
                    select0_cb.setVisibility(View.GONE);
                    select1_cb.setVisibility(View.GONE);
                    select2_cb.setVisibility(View.GONE);
                    select3_cb.setVisibility(View.GONE);
                    select4_cb.setVisibility(View.GONE);
                    select5_cb.setVisibility(View.GONE);
                    select6_cb.setVisibility(View.GONE);
                    select7_cb.setVisibility(View.GONE);
                    select8_cb.setVisibility(View.VISIBLE);
                    select8_cb.setChecked(true);
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, "8");
                    local_img.setBackground(context.getResources().getDrawable(R.mipmap.h1));
                }
                break;
            case R.id.selectlocal_rl:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                    checkPhonePermission();
                    if (autoFag) {
                        Crop.pickImage(this);
                    } else {
                        Toast.makeText(context, "权限被禁止!", Toast.LENGTH_LONG).show();
                    }
                } else {
                    VipDailog();
                }

                break;
            default:
                break;
        }
    }
    private void VipDailog() {
        final android.app.AlertDialog builder = new android.app.AlertDialog.Builder(context).create();

        builder.show();
        Window window = builder.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        window.setAttributes(params);// 设置生效
        window.setContentView(R.layout.dialog_alter);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        delete_ok.setText("注册");
        delete_ok.setTextColor(Color.RED);
        delete_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent newt = new Intent(context,PhoneCodeLoginActivity.class);
                newt.putExtra("type", 1);
                newt.putExtra("login",true);
                context.startActivity(newt);
            }
        });
        TextView cancle_tv = window.findViewById(R.id.cancle_tv);
        cancle_tv.setText("忽略");
        cancle_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
            }
        });
        TextView delete_tv = window.findViewById(R.id.delete_tv);

        delete_tv.setText("该功能是正式用户功能\n请注册正式用户");

    }
    private void xuanzhetup(String week) {
        final Dialog dialog = new Dialog(context, R.style.dialog_translucent);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        window.setAttributes(params);// 设置生效

        LayoutInflater fac = LayoutInflater.from(context);
        View more_pop_menu = fac.inflate(R.layout.dialog_xuanztup_xuanxiang, null);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(more_pop_menu);
        params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = getWindowManager().getDefaultDisplay().getWidth();
        dialog.show();

        TextView w = (TextView) more_pop_menu.findViewById(R.id.week);
        w.setText(week);

        more_pop_menu.findViewById(R.id.xiancge).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                checkPhonePermission();
                if (autoFag) {
                    Crop.pickImage(SetBackgroundActivity.this);
                } else {
                    Toast.makeText(context, "权限被禁止!", Toast.LENGTH_LONG).show();
                }
            }
        });

        more_pop_menu.findViewById(R.id.moren).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(SetBackgroundActivity.this, SetBackgroundMorenActivity.class);
                intent.putExtra("week_image", week_image);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {

            if (data != null) {
                String fileName = "BackGroundImage_" + String.valueOf(System.currentTimeMillis()) + ".png";

                Uri selectImageUri = data.getData();

                if (selectImageUri != null) {
                    Uri mDestinationUri = Uri.fromFile(new File(getCacheDir(), fileName));
                    UCrop uCrop = UCrop.of(selectImageUri, mDestinationUri);
                    UCrop.Options options = new UCrop.Options();
                    options.setHideBottomControls(true);
                    options.setCompressionQuality(50);
                    uCrop.withOptions(options);
                    uCrop.start(this);
                }

                select0_cb.setVisibility(View.GONE);
                select1_cb.setVisibility(View.GONE);
                select2_cb.setVisibility(View.GONE);
                select3_cb.setVisibility(View.GONE);
                select4_cb.setVisibility(View.GONE);
                select5_cb.setVisibility(View.GONE);
                select6_cb.setVisibility(View.GONE);
                select7_cb.setVisibility(View.GONE);
                select8_cb.setVisibility(View.GONE);


                if (requestCode == UCrop.REQUEST_CROP) {
                    final Uri resultUri = UCrop.getOutput(data);
                    String path = RxPhotoUtils.getRealFilePath(context, resultUri);
                    if (path != null) {
                        if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "").isEmpty()) {
                            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.LOCALPATH, path);
                        } else {
                            switch (week_image) {
                                case 1:
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE1, path);
                                    image0.setImageBitmap(BitmapFactory.decodeFile(path));
                                    break;
                                case 2:
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE2, path);
                                    image1.setImageBitmap(BitmapFactory.decodeFile(path));
                                    break;
                                case 3:
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE3, path);
                                    image2.setImageBitmap(BitmapFactory.decodeFile(path));
                                    break;
                                case 4:
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE4, path);
                                    image3.setImageBitmap(BitmapFactory.decodeFile(path));
                                    break;
                                case 5:
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE5, path);
                                    image4.setImageBitmap(BitmapFactory.decodeFile(path));
                                    break;
                                case 6:
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE6, path);
                                    image5.setImageBitmap(BitmapFactory.decodeFile(path));
                                    break;
                                case 7:
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.CUSTIOMMOSTIMAGE7, path);
                                    image6.setImageBitmap(BitmapFactory.decodeFile(path));
                                    break;
                            }
                            initview();
                        }
                    }
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /* 根据uri返回文件路径 */
    public String getPath(Uri uri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        // 好像是android多媒体数据库的封装接口，具体的看Android文档
        Cursor cursor = managedQuery(uri, proj, null, null, null);
        // 按我个人理解 这个是获得用户选择的图片的索引值
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        // 将光标移至开头 ，这个很重要，不小心很容易引起越界
        cursor.moveToFirst();
        // 最后根据索引值获取图片路径
        return cursor.getString(column_index);
    }

    boolean autoFag = false;
    private static final int RC_LOCATION_CONTACTS_PERM = 124;

    @AfterPermissionGranted(RC_LOCATION_CONTACTS_PERM)
    private void checkPhonePermission() {
        if (Build.VERSION.SDK_INT < 23) {
            autoFag = true;
        } else {
            String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE};
            if (EasyPermissions.hasPermissions(SetBackgroundActivity.this, perms)) {
                // Have permissions, do the thing!
                autoFag = true;
            } else {
                // Ask for both permissions
                EasyPermissions.requestPermissions(this, "该功能需要此权限，为了保证应用正常运行!",
                        RC_LOCATION_CONTACTS_PERM, perms);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // EasyPermissions handles the request result.
        if (PackageManager.PERMISSION_GRANTED == grantResults[0]) {
            autoFag = true;
        } else {
            autoFag = false;
        }
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Log.d("TAG", "onPermissionsGranted:" + requestCode + ":" + perms.size());
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
// (Optional) Check whether the user denied permissions and checked NEVER ASK AGAIN.
        // This will display a dialog directing them to enable the permission in app settings.
        EasyPermissions.checkDeniedPermissionsNeverAskAgain(this,
                getString(R.string.rationale_ask_again),
                R.string.action_settings, R.string.cancel, null, perms);
    }
}
